import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBChello {
    public static void main(String[] args) throws SQLException {
        //使用DateSource 描述MySQL服务器的位置
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/try_use_mysql?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("000000");

        //2.和数据库服务器建立连接  使用dateSourse.getConnectin来建立连接
        Connection connection = dataSource.getConnection();
       // System.out.println(connection);
        String sql = "insert into student (id, name)values(1,'张三')";
        PreparedStatement statement = connection.prepareStatement(sql);
        //
        int n = statement.executeUpdate();
        statement.close();
        connection.close();
    }
}
